package cn.ctyun.videoplayer.util;

public class ConvertUtils {
    private ConvertUtils() {
        throw new UnsupportedOperationException("cannot be instantiated");
    }

    public static byte[] hexStringToBytes(String hexString) {
        if (hexString != null && !hexString.equals("")) {
            hexString = hexString.toUpperCase();
            int length = hexString.length() / 2;
            char[] hexChars = hexString.toCharArray();
            byte[] d = new byte[length];

            for(int i = 0; i < length; ++i) {
                int pos = i * 2;
                d[i] = (byte)(charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1]));
            }

            return d;
        } else {
            return null;
        }
    }

    public static byte charToByte(char c) {
        return (byte)"0123456789ABCDEF".indexOf(c);
    }

    public static String bytesToHexString(byte[] b) {
        if (b.length == 0) {
            return null;
        } else {
            StringBuilder sb = new StringBuilder("");

            for(int i = 0; i < b.length; ++i) {
                int value = b[i] & 255;
                String hv = Integer.toHexString(value);
                if (hv.length() < 2) {
                    sb.append(0);
                }

                sb.append(hv);
            }

            return sb.toString();
        }
    }

    public static byte[] intToByte(int res) {
        byte[] targets = new byte[]{(byte)(res & 255), (byte)(res >> 8 & 255), (byte)(res >> 16 & 255), (byte)(res >>> 24)};
        return targets;
    }

    public static int byteToInt(byte[] res) {
        int targets = res[3] & 255 | res[2] << 8 & '\uff00' | res[1] << 16 & 16711680 | res[0] << 24 & -16777216;
        return targets;
    }

    public static String saveDecimals(int cnt, double value) {
        if (cnt == 2) {
            return String.format("%.02f", value);
        } else {
            return cnt == 1 ? String.format("%.01f", value) : String.format("%.0f", value);
        }
    }

    public static String nullOfString(String str) {
        if (str == null) {
            str = "";
        }

        return str;
    }

    public static byte stringToByte(String str) {
        byte b = 0;
        if (str != null) {
            try {
                b = Byte.parseByte(str);
            } catch (Exception var3) {
                ;
            }
        }

        return b;
    }

    public static boolean stringToBoolean(String str) {
        if (str == null) {
            return false;
        } else if (str.equals("1")) {
            return true;
        } else if (str.equals("0")) {
            return false;
        } else {
            try {
                return Boolean.parseBoolean(str);
            } catch (Exception var2) {
                return false;
            }
        }
    }

    public static int stringToInt(String str) {
        int i;
        if (str != null) {
            try {
                i = Integer.parseInt(str.trim());
            } catch (Exception var3) {
                i = 0;
            }
        } else {
            i = 0;
        }

        return i;
    }

    public static short stringToShort(String str) {
        short i;
        if (str != null) {
            try {
                i = Short.parseShort(str.trim());
            } catch (Exception var3) {
                i = 0;
            }
        } else {
            i = 0;
        }

        return i;
    }

    public static double stringToDouble(String str) {
        double i = 0.0D;
        if (str != null) {
            try {
                i = Double.parseDouble(str.trim());
            } catch (Exception var4) {
                i = 0.0D;
            }
        } else {
            i = 0.0D;
        }

        return i;
    }

    public static String intToString(int i) {
        String str = "";

        try {
            str = String.valueOf(i);
        } catch (Exception var3) {
            str = "";
        }

        return str;
    }

    public static long doubleToLong(double d) {
        long lo = 0L;

        try {
            lo = Long.parseLong(String.valueOf(d).substring(0, String.valueOf(d).lastIndexOf(".")));
        } catch (Exception var5) {
            lo = 0L;
        }

        return lo;
    }

    public static int doubleToInt(double d) {
        boolean var2 = false;

        int i;
        try {
            i = Integer.parseInt(String.valueOf(d).substring(0, String.valueOf(d).lastIndexOf(".")));
        } catch (Exception var4) {
            i = 0;
        }

        return i;
    }

    public static double longToDouble(long d) {
        double lo = 0.0D;

        try {
            lo = Double.parseDouble(String.valueOf(d));
        } catch (Exception var5) {
            lo = 0.0D;
        }

        return lo;
    }

    public static int longToInt(long d) {
        boolean var2 = false;

        int lo;
        try {
            lo = Integer.parseInt(String.valueOf(d));
        } catch (Exception var4) {
            lo = 0;
        }

        return lo;
    }

    public static long stringToLong(String str) {
        Long li = new Long(0L);

        try {
            li = Long.valueOf(str);
        } catch (Exception var3) {
            ;
        }

        return li;
    }

    public static String longToString(long li) {
        String str = "";

        try {
            str = String.valueOf(li);
        } catch (Exception var4) {
            ;
        }

        return str;
    }
}